﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Data;
using SIIE_ERP.BaseDatos.Configuracion;
using SIIE_ERP.BaseDatos;
using SIIE_ERP.Codigo.Login;

namespace SIIE_ERP.Codigo
{
    public static class ValidacionCodigo
    {
        public static bool ValidacionCodigoPostal(string codigoPostal)
        {
            try
            {
                if (codigoPostal.Count() == 5)
                {
                    int codigoP = Convert.ToInt32(codigoPostal.Trim());
                    return true;
                }
                return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool ValidacionCorreoElectronico(string correoElectronico)
        {
            string expresion = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
            if (Regex.IsMatch(correoElectronico, expresion))
            {
                if (Regex.Replace(correoElectronico, expresion, String.Empty).Length == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }

        }

        public static DataTable ValidacionRFCLicencia(int IdUsuario, string UserName)
        {
            DataTable RFCs = dbo_Sec_Usuario_RFC.Get_Sec_Usuario_RFC_By_Id(IdUsuario);
            DataTable RFCL = new DataTable();
            DataColumn col;

            col = new DataColumn();
            col.DataType = System.Type.GetType("System.String");
            col.ColumnName = "RFC";
            RFCL.Columns.Add(col);
            for (int i = 0; i < RFCs.Rows.Count; i++)
            {
                string[] CadenaLicencia = CypherClass.DescifrarTexto(RFCs.Rows[i]["Genero"].ToString()).Split('|');


                DateTime Fecha_Licencia = Convert.ToDateTime(CadenaLicencia[0].ToString());
                if (UserName == CadenaLicencia[1].ToString() && RFCs.Rows[i]["RFC"].ToString() == CadenaLicencia[2].ToString())
                {
                    if (Convert.ToDateTime(CadenaLicencia[0]) > DateTime.Now)
                    {
                        RFCL.Rows.Add(RFCs.Rows[i]["RFC"].ToString());
                    }
                }

            }
            return RFCL;

        }
    }
}
